<template>
  <view class="main">
    <view class="logintop">
      <view class="fontsize24">欢迎登录患者端</view>
    </view>
    <view class="fontsize14">
      <view class="inputview">
        <input
          type="number"
          maxlength="11"
          v-model="loginquery.phone"
          value=""
          placeholder="请输入手机号"
        />
      </view>
      <view class="inputview flexrow flexbetween">
        <input
          type="text"
          value=""
          v-model="loginquery.code"
          placeholder="请输入验证码"
        />
        <span class="color3FA" v-if="time == 0" @click="getyzm"
          >获取验证码</span
        >
        <span v-else class="color999">{{ time }}s</span>
      </view>
    </view>
    <view class="loginbtn" @click="loginsumbit"> 登录 </view>
    <view class="fontsize12 tyment">
      <span @click="tocheck">
        <template v-if="!checkflage">
          <image
            src="https://applet.youtuyiliao.com/profile/upload/xiaochengxu/static/login/nochoose.png"
            mode=""
          ></image>
        </template>
        <template v-else>
          <image
            src="https://applet.youtuyiliao.com/profile/upload/xiaochengxu/static/login/choose.png"
            mode=""
          ></image>
        </template>
      </span>
      <span>我已同意</span>
      <span class="color3FA" @click="tProtocol(1)">《注册协议》</span>
      <span @click="tProtocol(2)" class="color3FA">《隐私协议》</span>
    </view>
    <!-- <view class="flexcolumn flexcenter weixincon">
			<view class="flexrow flexcenter weixintop">
				<view></view>
				<view class="color999 fontsize14">第三方登录</view>
				<view></view>
			</view>
			<view class=" weixinbot">
				<image src="https://applet.youtuyiliao.com/profile/upload/xiaochengxu/static/login/weixin.png" mode=""></image>
			</view>
		</view> -->
  </view>
</template>

<script>
import { sendPhoneCode, login } from '../../api/index.js'
import { genTestUserSig } from "@/im/GenerateTestUserSig.js";
export default {
  data() {
    return {
      checkflage: false,
      time: 0,
      loginquery: {
        phone: '',//	非必选	String	账号
        code: '',//		非必选	Integer	验证码
        type: 1//1:登录2:修改手机号
      },
    }
  },
  onShow() {
    console.log(this.time)
  },
  onUnload() {
    this.$store.commit("setStartLoaded", true);
  },
  methods: {
    //查看协议
    tProtocol(e) {
      uni.navigateTo({
        url: 'agreement?category=' + e
      })
    },
    //登录
    loginsumbit() {
      if (!this.phoneFun(this.loginquery.phone)) {
        this.$toast('请输入正确的手机号')
        return
      }
      if (!this.loginquery.code) {
        this.$toast('请输入验证码')
        return
      }
      if (!this.checkflage) {
        this.$toast('请阅读协议并同意')
        return
      }
      login(this.loginquery).then(res => {
        console.log(res)
        uni.setStorageSync('token', res.data.token)
        uni.setStorageSync('userId', res.data.userId)
        uni.setStorageSync('nickName', res.data.nickName)
        uni.setStorageSync('headImage', res.data.headImage)
        const { userSig } = genTestUserSig(res.data.userId || "");
        this.$im.login(res.data.userId || "", userSig || "").then(res => {
          this.$utils.showToast("登录成功", true, () => {
            uni.reLaunch({
              url: "/chronicDisease/pages/index/index",
            });
          });
        })

      })
    },
    //获取验证码
    getyzm() {
      if (!this.phoneFun(this.loginquery.phone)) {
        this.$toast('请输入正确的手机号')
        return
      }
      this.time = 60
      sendPhoneCode(this.loginquery).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$toast('已发送验证码，请注意查收')
          // this.loginquery.code=res.data.code
        }
      })
      var interval = setInterval(() => {
        this.time--
        if (this.time == 0) {
          clearInterval(interval)
        }
      }, 1000)
    },
    tocheck() {
      this.checkflage = !this.checkflage
    },


    getUserInfo() {
      return new Promise((resolve, reject) => {
        uni.getUserProfile({
          lang: 'zh_CN',
          desc: '用户登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，
          success: (res) => {
            console.log(res, 'resss')
            resolve(res.userInfo)
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    },

    getLogin() {
      return new Promise((resolve, reject) => {
        uni.login({
          success(res) {
            console.log(res, 'res')
            resolve(res)
          },
          fail: (err) => {
            console.log(err, 'logoer')
            reject(err)
          }
        })
      })
    },

    weixinLogin() {
      let that = this;
      uni.getProvider({
        service: 'oauth',
        success: function (res) {
          //支持微信、qq和微博等
          if (~res.provider.indexOf('weixin')) {
            console.log(res, 'ress')
            let userInfo = that.getUserInfo();
            let loginRes = that.getLogin();
            Promise.all([userInfo, loginRes]).then((result) => {
              console.log(result)
              let userInfo = result[0];
              let loginRes = result[1];
              let access_token = loginRes.authResult.access_token;
              let openid = loginRes.authResult.openid;
              let data = Object.assign(loginRes.authResult, userInfo);
              wxlogin({
                nikeName: userInfo.nikeName,
                wxOpenId: loginRes.authResult.openid,
                headImage: userInfo.avatarUrl
              }).then(res => {
                if (res.data.ifNewUser == 1) {
                  uni.navigateTo({
                    url: '../my/newPhone'
                  })
                } else {
                  uni.reLaunch({
                    url: '../my/my'
                  })
                }
              })
            })
          }
        },
        fail: function (err) {
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: err
          })
        }
      })
    },
  }
}
</script>

<style>
/* @import url("@/chronicDisease/style/common.scss"); */
.main {
  width: 90vw;
  margin: 0 auto;
}
.register {
  margin: 28rpx 0 68rpx 0;
}
.tyment image {
  width: 48rpx;
  height: 48rpx;
  margin-right: 8rpx;
  vertical-align: -7rpx;
}
.logintop {
  margin: 100rpx 0 20rpx 0;
}
.logintop_bot {
  height: 70rpx;
  line-height: 70rpx;
}
.inputview {
  height: 132rpx;
  border-bottom: 1px solid #f7f7f7;
}
.inputview input {
  height: 132rpx;
  font-size: 28rpx;
}
.tyment {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-top: 26rpx;
  justify-content: center;
}
.loginbtn {
  height: 96rpx;
  line-height: 96rpx;
  width: 90vw;
  text-align: center;
  color: #ffffff;
  border-radius: 12rpx;
  background-color: #3fa3fa;
  font-size: 28rpx;
  margin-top: 68rpx;
}
.weixincon {
  width: 90vw;
  position: fixed;
  bottom: 80rpx;
}
.weixintop > view:nth-child(1),
.weixintop > view:nth-child(3) {
  width: 100rpx;
  height: 2rpx;
  background-color: #d4d4d4;
  margin: 0 12rpx;
}
.weixinbot image {
  width: 100rpx;
  height: 100rpx;
  margin-top: 32rpx;
}
</style>
